package com.formiaomiao.demain.service;

import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.receiver.NotificationClickReceiver;

public class PushIntentService extends IntentService {

    private int notificationId = 0;
    Notification notification;

    public PushIntentService() {
        super("BadgeIntentService");
    }

    private NotificationManager mNotificationManager;

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @TargetApi(Build.VERSION_CODES.O)
    @Override
    protected void onHandleIntent(Intent intent) {
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (intent != null) {
            final String data = intent.getStringExtra("data");
            final String name = intent.getStringExtra("name");
            try {
                mNotificationManager.cancel(notificationId);
                notificationId++;
                Intent clickIntent = new Intent(PushIntentService.this, NotificationClickReceiver.class); //点击通知之后要发送的广播
                int id = (int) (System.currentTimeMillis() / 1000);
                PendingIntent contentIntent = PendingIntent.getBroadcast(this.getApplicationContext(), id, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                //判断当前系统版本是否为8.0
                if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
                    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
                    NotificationChannel channel = new NotificationChannel("1",
                            "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
                    channel.enableLights(true); //是否在桌面icon右上角展示小红点
                    channel.setLightColor(Color.RED); //小红点颜色
                    channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
                    notificationManager.createNotificationChannel(channel);
                    Notification.Builder builder = new Notification.Builder(this,"1"); //与channelId对应
                    //icon title text必须包含，不然影响桌面图标小红点的展示
                    builder.setSmallIcon(android.R.drawable.stat_notify_chat)
                            .setContentTitle(name)
                            .setContentText(data)
                            .setContentIntent(contentIntent)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setAutoCancel(true)
                            .setNumber(3); //久按桌面图标时允许的此条通知的数量
                    notification = builder.build();
                }else{
                    Notification.Builder builder = new Notification.Builder(getApplicationContext())
                            .setContentTitle(name)
                            .setContentText(data)
                            .setContentIntent(contentIntent)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setAutoCancel(true);
                    notification = builder.build();
                }
                mNotificationManager.notify(notificationId, notification);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
        }
    }

}
